#pragma once

#include <iostream>
#include<signal.h>
#include "TcpSverver.hpp"
#include "Protocol.hpp"
#include"Task.hpp"
#include"ThreadPool.hpp"

#define PORT 8080

class HttpServer
{
private:
    int _port;
    bool quit;

public:
    HttpServer(int port = PORT)
        : _port(port), quit(false)
    {
    }

    void InitServer()
    {
        //忽略13号信号，提高效能
        signal(SIGPIPE,SIG_IGN);
    }

    void Loop()
    {
        TcpServer *tsev = TcpServer::GetInstance(_port);
        LOG(INFO, "Loop begin");
        while (!quit)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sock = accept(tsev->Sock(), (struct sockaddr *)&peer, &len);

            if (sock < 0)
                continue;

            LOG(INFO, "Get a new link");

            Task task(sock);
            ThreadPool::GetInstance()->PushTask(task);

        }
    }

    ~HttpServer() {}
};